const { dayjs } = global

Component({
  options: {
    multipleSlots: true
  },
  properties: {
    padding: {
      type: String,
      value: '12px'
    },
    ricon: {
      type: String,
      value: ''
    },
    thairline: {
      type: Boolean,
      value: false
    },
    bhairline: {
      type: Boolean,
      value: true
    },
  },
  data: {
    uiShow: false,
    params: null,

    curYear: '',
    curMonth: '',
    dateFormat: '',
    date: '',
    dateFormat: '',
    dates: [],
    parts: [],

    range: false,
    startDate: '请选择',
    endDate: '请选择'
  },
  methods: {
    handleHide(e) {
      // console.log('handleHide', this.data)
      this.setData({ uiShow: false })
      this.triggerEvent('hideEvent', this.data)
    },
    handleShow(e, params) {
      console.log('handleShow', params)
      const { startDate, endDate, title, range } = params
      const parts = []
      startDate && parts.push(dayjs(startDate.replace(/-/g, '/')))
      endDate && parts.push(dayjs(endDate.replace(/-/g, '/')))

      this.setData({
        uiShow: true,
        startDate: startDate || '请选择',
        endDate: endDate || '请选择',
        parts, title, range
      })
      this.handleCurrMonth(0)
      this.triggerEvent('showEvent')
    },
    handleConfirm(e) {
      const that = this
      this.triggerEvent('confirmEvent', {
        data: that.data,
        cb() {
          that.setData({ uiShow: false })
        }
      })
    },

    handlePrevMonth(e) {
      this.handleCurrMonth(-1)
    },
    handleNextMonth(e) {
      this.handleCurrMonth(1)
    },
    handleCurrMonth(offset) {
      const oldDate = this.data.date || dayjs()
      const newDate = oldDate.add(offset, 'month')

      const t1 = newDate.startOf('month')
      const t2 = newDate.endOf('month')
      const format = 'YYYY-MM-DD'
      const prevLength = t1.$W
      const nextLength = 6 - t2.$W
      const procLength = t2.$D

      const curYear = t1.$y
      const curMonth = t1.$M
      const curDate = t1.$D
      const date = t1
      const dateFormat = t1.format('YYYY年MM月')

      const dates = []
      const startDate = t1.add(-prevLength, 'day')
      for (let i = 0; i < prevLength + procLength + nextLength; i++) {
        dates.push({
          date: startDate.add(i, 'day'),
          inMonth: i >= prevLength && i < (procLength + prevLength),
          inPart: false,
          isStopStart: false,
          isStopEnd: false,
          isOne: false
        })
      }
      this.setData({ curYear, curMonth, curDate, date, dates, dateFormat })
      this.handleUI()
    },
    handleCellTap(e) {
      const { range } = this.data
      const { date } = e.currentTarget.dataset
      const tapDate = dayjs(`${date.$y}-${date.$M + 1}-${date.$D}`.replace(/-/g, '/')) //.format('YYYY-MM-DD')
      let { parts } = this.data

      if (!range) {
        parts[0] = tapDate
        const startDate = tapDate.format('YYYY-MM-DD') || '请选择'
        this.setData({ parts, startDate })
      } else {
        if (parts.length === 2) {
          const flag = parts[0].isBefore(parts[1])
          const t1 = flag ? parts[0] : parts[1]
          const t2 = flag ? parts[1] : parts[0]

          if (tapDate.isBefore(t1)) {
            parts = [t2, tapDate]
          }
          if (t2.isBefore(tapDate)) {
            parts = [t1, tapDate]
          }
          if (tapDate.isBefore(t2) && t1.isBefore(tapDate)) {
            parts.push(tapDate)
            parts = parts.splice(1, 2)
          }
          if (tapDate.isSame(t1) || tapDate.isSame(t2)) {
            parts = [tapDate, tapDate]
          }
        } else {
          parts.push(tapDate)
        }

        const sortParts = parts.map(i => i).sort((a, b) => b.isBefore(a))
        const startDate = sortParts[0] && sortParts[0].format('YYYY-MM-DD') || '请选择'
        const endDate = sortParts[1] && sortParts[1].format('YYYY-MM-DD') || '请选择'

        this.setData({ parts, startDate, endDate })
      }
      this.handleUI()
    },
    handleUI() {
      const { parts, dates } = this.data
      const format = 'YYYY-MM-DD'
      dates.map(item => {
        const i = item.date
        i.isStopStart = false
        i.isStopEnd = false
        i.isOne = false
        if (parts.length === 0) {
          i.inPart = false
        }
        if (parts.length === 1) {
          // i.inPart = i.isSame(parts[0])
          i.isOne = i.isSame(parts[0])
        }
        if (parts.length === 2) {
          const flag = parts[0].isBefore(parts[1])
          const t1 = flag ? parts[0] : parts[1]
          const t2 = flag ? parts[1] : parts[0]


          i.inPart = i.isBefore(t2) && t1.isBefore(i)


          if ((i.isSame(t1) || i.isSame(t2)) && i.isSame(t1)) {
            i.isStopStart = true
          }
          if ((i.isSame(t1) || i.isSame(t2)) && i.isSame(t2)) {
            i.isStopEnd = true
          }


        }
      })
      this.setData({ dates })
    }
  },
  lifetimes: {
    ready() {
      this.handleCurrMonth(0)
    }
  }
})
